﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using VietKinsNutrition.Data;
using System.Data;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_NamhocBLL : ClsBindingBLL
    {
        private int _id;

        ClsTblDm_NamhocDAL ClsTblDm_Namhocdal = new ClsTblDm_NamhocDAL();

        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
            }
        }

        public DataTable TableNamhocSelectAll()
        {
            return this.ClsTblDm_Namhocdal.TableNamhocSelectAll();
        }

        public DataTable ReturnTable_Tennamhoc()
        {
            return this.ClsTblDm_Namhocdal.ReturnTable_Tennamhoc();
        }
        public DataTable ReturnTable_Namhoctiep()
        {
            return this.ClsTblDm_Namhocdal.TblNamhoctiep();
        }

        public void BindingComboNamhocIdByNamhocCurrent(ComboBox Cbo)
        {
            Cbo.DataSource = ClsTblDm_Namhocdal.BindingComboNamhocIdByNamhocCurrent();
            Cbo.DisplayMember = "Namhoc";
            Cbo.ValueMember = "Id";
        }

        public bool FindSameName(int iBtn, string VarTen)
        {
            bool Value = false;
            switch (iBtn)
            {
                case 1:
                    {
                        if (this.ClsTblDm_Namhocdal.FindSameName(VarTen).Tables[0].Rows.Count >= 1)
                        {
                            MessageBox.Show("Không cho phép thêm trùng dữ liệu", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            Value = false;
                        }
                        else
                        {

                            Value = true;
                        }
                        break;
                    }
                case 2:
                    {
                        if (this.ClsTblDm_Namhocdal.FindSameName(VarTen).Tables[0].Rows.Count == 0)
                        {
                            return true;
                        }
                        DataRow Row = this.ClsTblDm_Namhocdal.FindSameName(VarTen).Tables[0].Rows[0];

                        if (this.ClsTblDm_Namhocdal.FindSameName(VarTen).Tables[0].Rows.Count >= 1 && this.Id != int.Parse(Row["Id"].ToString()))
                        {
                            MessageBox.Show("Tên không cho phép trùng", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            Value = false;
                        }
                        else
                        {
                            Value = true;
                        }
                        break;
                    }
            }
            return Value;
        }

        public void FocusControl(TextBox Textbox, DataGridView Grid)
        {
            Textbox.Focus();
            Grid.Enabled = !Grid.Enabled;
        }

        public void BingSource_InputControl(TextBox Nambatdau, TextBox Namketthuc, CheckBox Checkbox, DataGridView Grid)
        {
            if (Grid.RowCount > 0)
            {
                this.Id = int.Parse(Grid.Rows[Grid.CurrentRow.Index].Cells["Id"].Value.ToString());
                Nambatdau.Text = Grid.Rows[Grid.CurrentRow.Index].Cells["Nambatdau"].Value.ToString();
                Namketthuc.Text = Grid.Rows[Grid.CurrentRow.Index].Cells["Namketthuc"].Value.ToString();
                Checkbox.Checked = bool.Parse(Grid.Rows[Grid.CurrentRow.Index].Cells["Trangthai"].Value.ToString());
            }
        }

        public void ClearControl(TextBox Nambatdau, TextBox Namketthuc, CheckBox Checkbox)
        {
            Nambatdau.Text = "";
            Namketthuc.Text = "";
            Checkbox.Checked = false;
        }
        public void BingSource_DataGrid(DataGridView GridView)
        {
            GridView.DataSource = this.ClsTblDm_Namhocdal.SelectAll();
        }
        public int ReturnNamhocId()
        {
            return this.ClsTblDm_Namhocdal.ReturnNamhocId();
        }
        public string ReturnTenNamhoc()
        {
            return this.ClsTblDm_Namhocdal.ReturnTenNamhoc();
        }
        public void Insert(int Nambatdau, int Namketthuc, string Tennamhoc, bool Trangthai)
        {
            this.ClsTblDm_Namhocdal.Insert(Nambatdau, Namketthuc, Tennamhoc, Trangthai);
        }
        public void Edit(int Id, int Nambatdau, int Namketthuc, string Tennamhoc, bool Trangthai)
        {
            this.ClsTblDm_Namhocdal.Edit(Id, Nambatdau, Namketthuc, Tennamhoc, Trangthai);
        }
        public DataTable Namhientai()
        {

            return this.ClsTblDm_Namhocdal.TblNamhocByCurrent();
        }
    }
}
